angular.module('mustache.controllers', [])
	.controller('registerCtrl', function($scope, $state, xhr){
		"用户注册控制器";
		$scope.register = {}
		$scope.submit = function(){
			xhr.service('post', {model: 'MustachUser', module: 'register', data: $scope.register}, function(resp){
				if(resp.type.toLowerCase()=='success') $state.go('app.my');
			})
		}
	})
	.controller('loginCtrl', function($scope, $window, $ionicLoading, $state, xhr){
		"用户登录控制器"
		$scope.login = {}
		$scope.submit = function(){
			xhr.service('post', {model: 'MustachUser', module: 'login', data: $scope.login}, function(resp){
				if(resp.type.toLowerCase()=='success') $state.go('app.my', {}, { reload: true });
			})
		}
	})
	.controller('applyRealnameCtrl', function($scope, $state, $ionicLoading, xhr, imgPickerService, ngSwal){
		xhr.userState('register', function(user){
			$scope.realname = {
				realname: user.realname,
				idcard: user.idcard,
				idcard1: user.idcard_url_1,
				idcard2: user.idcard_url_2
			};
			$scope.setIdcard1 = function(){
				imgPickerService.camera(function(res){
					$scope.idcard1 = res;
				})
			};
			$scope.setIdcard2 = function(){
				imgPickerService.camera(function(res){
					$scope.idcard2 = res;
				})
			};
			$scope.submit = function(){
				if($scope.idcard1 == 'img/notImg.jpg'){
					ngSwal.alert({text: '身份证正面必须拍照', type: 'warning'});
				}else{
					if($scope.idcard2 == 'img/notImg.jpg'){
						ngSwal.alert({text: '身份证背面必须拍照', type: 'warning'});
					}else{
						imgPickerService.uploads([$scope.idcard1, $scope.idcard2], '上传二代身份证图片', function(resp){
							$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
							$scope.realname.idcard_1 = resp[0].data.id;
							$scope.realname.idcard_2 = resp[1].data.id;
							xhr.service('post', {
								model: 'MustachUser',
								module: 'realnameApply',
								data: $scope.realname
							}, function(){
								$ionicLoading.hide();
								$state.go('app.my', {}, {reload: true});
							});
						});
					}
				}
			}
		}, true);
	})
	.controller('applyEngineerCtrl', function($scope, $ionicLoading, $state, $http, xhr, imgPickerService){
		xhr.userState('realname', function(user){
			var storage = xhr.getStorage();
			$scope.categoryList = storage.category.data;
			$scope.certificateNum = 9
			$scope.categoryObj = {};
			$scope.engineer = {
				certificate: [],
			};
			$scope.cateChange = function(){
				$scope.engineer.cid = $scope.engineer.category.id;
				$scope.engineer.types = $scope.engineer.category.name;
			}
			$scope.certificate =[];
			$scope.certificateSurplus = $scope.certificateNum - $scope.certificate.length;
			$scope.addCertificate = function(){
				imgPickerService.photo(function(result){
					$scope.certificate.push(result);
					$scope.certificateSurplus = $scope.certificateNum - $scope.certificate.length;
					jq('#certificateImg>div').append('<img src="'+result+'" style="width:180px;height: 120px; padding:5px;">');
				});
			}
			$scope.submit = function(){
				imgPickerService.uploads($scope.certificate, '上传工程师认证图片', function(resp){
					$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
					for(var i=0; i<resp.length; i++){
						$scope.engineer.certificate.push(resp[i].data.id);
					}
					$scope.engineer.adept = JSON.stringify($scope.adept);
					$scope.adepts = function(){
						console.log(JSON.stringify($scope.adept), $scope.adept);
					}
					xhr.service('post', {model: 'MustachUser', module: 'engineerApply', data: $scope.engineer}, function(){
						$ionicLoading.hide();
						$state.go('app.my', {}, {reload: true});
					});
				});
			}
		}, true);
	})
	.controller('MyCtrl', function($scope, xhr, imgPickerService){
		"我的控制器";
		xhr.userState('list', function(user, identity){
			$scope.state = identity;
			$scope.user = user;
			$scope.setHeadimg = function () {
				if(identity.isLogin){
					imgPickerService.headimg(function(url){
						imgPickerService.uploads([url], '上传个人头像图片', function(resp){
							xhr.service('post', {model: 'MustachUser', module: 'setHeadimg', data: {headimg: resp[0].data.id}});
						});
					})
				}
			}
			/*$scope.bindWeixin = function(){
				xhr.bindWeixin();
			}*/
		});
	})
	.controller('PushTaskCtrl', function($scope, $ionicLoading, $state, xhr, imgPickerService){
		xhr.userState('register', function(user){
			var storage = xhr.getStorage();
			$scope.pictureNum = 9
			$scope.picture =[];
			$scope.form = {
				extend: {},
				picture: []
			};
			$scope.categoryList = storage.category.data;
			$scope.pictureSurplus = $scope.pictureNum - $scope.form.picture.length;
			$scope.addPicture = function(){
				imgPickerService.photo(function(result){
					$scope.picture.push(result);
					$scope.pictureSurplus = $scope.pictureNum - $scope.picture.length;
					jq('#pictureImg>div').append('<img src="'+result+'" style="width:180px;height: 120px; padding:5px;">');
				});
			}
			$scope.cateChange = function(){
				if($scope.form.categoryObj.settle.is_section == 0)
					$scope.form.money = $scope.form.categoryObj.settle.price;
			}
			$scope.change = function(index){
				var field = $scope.form.categoryObj.advanced_field[index];
				if($scope.form.categoryObj.settle.correlation == field.id){
					var section = $scope.form.categoryObj.settle.section;
					section.forEach(function(ele){
						if($scope.form.extend[field.field] >= ele.min && $scope.form.extend[field.field] < ele.max)
							$scope.form.money = ele.price;
					})
				}
			}
			$scope.submit = function(){
				imgPickerService.uploads($scope.picture, '上传任务相关图片', function(resp){
					$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
					var category = $scope.form.categoryObj;
					$scope.form.cid = category.id;
					category.advanced_field.forEach(function(ele){
						if(ele.types == 'select')
							$scope.form.extend[ele.field].types = ele.types;
						else
							$scope.form.extend[ele.field] = {label: ele.label, ckey: ele.ckey, value: $scope.form.extend[ele.field], types: ele.types};
					});
					$scope.form.extend = JSON.stringify($scope.form.extend)
					delete $scope.form.categoryObj;
					for(var i=0; i<resp.length; i++){
						$scope.form.picture.push(resp[i].data.id);
					}
					xhr.service('post', {model: 'MustachTask', module: 'taskAction', data: $scope.form}, function(res){
						$ionicLoading.hide();
						$state.go('app.tasks', {}, {reload: true});
					});
				});
			}
		}, true);
	})
	.controller('HomeCtrl', function($scope, $location, $state, xhr) {
		$scope.$emit('menu', $location.path());
		var storage = xhr.getStorage();
		$scope.search = function(){
			$state.go('app.taskSearch', {query: $scope.query}, {reload: true})
		}
		if(angular.isUndefined(storage.sitedate) || storage.sitedate === null){
			$scope.advert = [
				{url: 'img/banner/banner-a.jpg'},
				{url: 'img/banner/banner-b.jpg'},
				{url: 'img/banner/banner-c.jpg'},
				{url: 'img/banner/banner-d.jpg'},
			];
		}else{
			$scope.advert = storage.sitedate.data.advert;
		}
		for (var i = 0; i <= 6; i++) {
			var taskc1cases = [
				{name: '验收工程', pic:'img/taskcase/case1.jpg', payfor:'0.5千'},
				{name: '设计咨询', pic:'img/taskcase/case2.jpg', payfor:'2千'},
				{name: '水电验收', pic:'img/taskcase/case3.jpg', payfor:'2.3千'},
				{name: '新房验收', pic:'img/taskcase/case4.jpg', payfor:'1.2千'},
				{name: '环境监测', pic:'img/taskcase/case5.jpg', payfor:'3.3千'},
				{name: '环境监测', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.items =taskc1cases;
			var taskc2cases = [
				{name: '新房验收', pic:'img/taskcase/case6.jpg', payfor:'0.5千'},
				{name: '新房验收', pic:'img/taskcase/case7.jpg', payfor:'2千'},
				{name: '新房验收', pic:'img/taskcase/case8.jpg', payfor:'2.3千'},
				{name: '新房验收', pic:'img/taskcase/case9.jpg', payfor:'1.2千'},
				{name: '新房验收', pic:'img/taskcase/case10.jpg', payfor:'3.3千'},
				{name: '新房验收', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.c2items =taskc2cases;
			var taskc3cases = [
				{name: '预算审核', pic:'img/taskcase/case1.jpg', payfor:'0.5千'},
				{name: '预算审核', pic:'img/taskcase/case2.jpg', payfor:'2千'},
				{name: '预算审核', pic:'img/taskcase/case3.jpg', payfor:'2.3千'},
				{name: '预算审核', pic:'img/taskcase/case4.jpg', payfor:'1.2千'},
				{name: '预算审核', pic:'img/taskcase/case5.jpg', payfor:'3.3千'},
				{name: '预算审核', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.c3items =taskc3cases;
			var taskc4cases = [
				{name: '材料验收', pic:'img/taskcase/case1.jpg', payfor:'0.5千'},
				{name: '材料验收', pic:'img/taskcase/case2.jpg', payfor:'2千'},
				{name: '材料验收', pic:'img/taskcase/case3.jpg', payfor:'2.3千'},
				{name: '材料验收', pic:'img/taskcase/case4.jpg', payfor:'1.2千'},
				{name: '材料验收', pic:'img/taskcase/case5.jpg', payfor:'3.3千'},
				{name: '材料验收', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.c4items =taskc4cases;
			var taskc5cases = [
				{name: '专业咨询', pic:'img/taskcase/case1.jpg', payfor:'0.5千'},
				{name: '专业咨询', pic:'img/taskcase/case2.jpg', payfor:'2千'},
				{name: '专业咨询', pic:'img/taskcase/case3.jpg', payfor:'2.3千'},
				{name: '专业咨询', pic:'img/taskcase/case4.jpg', payfor:'1.2千'},
				{name: '专业咨询', pic:'img/taskcase/case5.jpg', payfor:'3.3千'},
				{name: '专业咨询', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.c5items = taskc5cases;
			var taskc6cases = [
				{name: '水电验收', pic:'img/taskcase/case1.jpg', payfor:'0.5千'},
				{name: '水电验收', pic:'img/taskcase/case2.jpg', payfor:'2千'},
				{name: '水电验收', pic:'img/taskcase/case3.jpg', payfor:'2.3千'},
				{name: '水电验收', pic:'img/taskcase/case4.jpg', payfor:'1.2千'},
				{name: '水电验收', pic:'img/taskcase/case5.jpg', payfor:'3.3千'},
				{name: '水电验收', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.c6items = taskc6cases;
			var taskc7cases = [
				{name: '现场核量', pic:'img/taskcase/case1.jpg', payfor:'0.5千'},
				{name: '现场核量', pic:'img/taskcase/case2.jpg', payfor:'2千'},
				{name: '现场核量', pic:'img/taskcase/case3.jpg', payfor:'2.3千'},
				{name: '现场核量', pic:'img/taskcase/case4.jpg', payfor:'1.2千'},
				{name: '现场核量', pic:'img/taskcase/case5.jpg', payfor:'3.3千'},
				{name: '现场核量', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.c7items = taskc7cases;
			var taskc8cases = [
				{name: '竣工验收', pic:'img/taskcase/case1.jpg', payfor:'0.5千'},
				{name: '竣工验收', pic:'img/taskcase/case2.jpg', payfor:'2千'},
				{name: '竣工验收', pic:'img/taskcase/case3.jpg', payfor:'2.3千'},
				{name: '竣工验收', pic:'img/taskcase/case4.jpg', payfor:'1.2千'},
				{name: '竣工验收', pic:'img/taskcase/case5.jpg', payfor:'3.3千'},
				{name: '竣工验收', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.c8items =taskc8cases;
			var taskc9cases = [
				{name: '环境监测', pic:'img/taskcase/case1.jpg', payfor:'0.5千'},
				{name: '环境监测', pic:'img/taskcase/case2.jpg', payfor:'2千'},
				{name: '环境监测', pic:'img/taskcase/case3.jpg', payfor:'2.3千'},
				{name: '环境监测', pic:'img/taskcase/case4.jpg', payfor:'1.2千'},
				{name: '环境监测', pic:'img/taskcase/case5.jpg', payfor:'3.3千'},
				{name: '环境监测', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.c9items =taskc9cases;
			var taskc10cases = [
				{name: '过程验收', pic:'img/taskcase/case1.jpg', payfor:'0.5千'},
				{name: '过程验收', pic:'img/taskcase/case2.jpg', payfor:'2千'},
				{name: '过程验收', pic:'img/taskcase/case3.jpg', payfor:'2.3千'},
				{name: '过程验收', pic:'img/taskcase/case4.jpg', payfor:'1.2千'},
				{name: '过程验收', pic:'img/taskcase/case5.jpg', payfor:'3.3千'},
				{name: '过程验收', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.c10items = taskc10cases;
			var taskc11cases = [
				{name: '新房验收', pic:'img/taskcase/case1.jpg', payfor:'0.5千'},
				{name: '新房验收', pic:'img/taskcase/case2.jpg', payfor:'2千'},
				{name: '新房验收', pic:'img/taskcase/case3.jpg', payfor:'2.3千'},
				{name: '新房验收', pic:'img/taskcase/case4.jpg', payfor:'1.2千'},
				{name: '新房验收', pic:'img/taskcase/case5.jpg', payfor:'3.3千'},
				{name: '新房验收', pic:'img/taskcase/case5.jpg', payfor:'1.3千'}
			];
			$scope.c11items =taskc11cases;
			// $scope.doRefresh = function(){
			//   $http.get('json/taskcases.json')
			//     .success(function(newItems){
			//       $scope.items = newItems;
			//     })
			//     .finally(function(){
			//       $scope.$broadcast('scroll.refreshComplete');
			//     });
			// };
		};
	})
	.controller('TasksCtrl', function($scope, $ionicLoading, $http, xhr) {
		var p =1;
		var storage = xhr.getStorage();
		if((angular.isUndefined(storage.category) || storage.category === null)){
			$http({
				url: baseUrl + '/Restful/angular.html',
				cache: false,
				timeout: 30000,
				method: 'get',
				params: {model: 'MustachTask', module: 'categoryCache', data: {reload: true}}
			}).success(function(resp){
				if(resp.type == 'Success'){
					$scope.categoryList = resp.data;
				}
			});
		}else{
			$scope.categoryList = storage.category.data;
		}
		$scope.onSlideMove = function(index){
			$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
			xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: p, cid: index.cid}}, function(resp){
				if(resp.type == 'Success'){
					$scope.tasks = resp.data;
					$scope.totalPages = resp.page.totalPages.value;
				}else{
					$scope.tasks = new Array;
					$scope.totalPages = 0;
				}
				$ionicLoading.hide();
			});
			$scope.doRefresh = function(){
				$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
				xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: 1, cid: index.cid}}, function(resp){
					if(resp.type == 'Success'){
						$scope.tasks = resp.data;
						$scope.totalPages = resp.page.totalPages.value;
					}else{
						$scope.tasks = new Array;
						$scope.totalPages = 0;
					}
					$ionicLoading.hide();
					$scope.$broadcast('scroll.refreshComplete');
				});
			};
			$scope.loadMore = function(){
				if($scope.totalPages > ++p){
					$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
					xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: p, cid: index.cid}}, function(resp){
						if(resp.type == 'Success'){
							$scope.tasks = resp.data;
							$scope.totalPages = resp.page.totalPages.value;
						}else{
							$scope.tasks = new Array;
							$scope.totalPages = 0;
						}
						$ionicLoading.hide();
						$scope.$broadcast('scroll.infiniteScrollComplete');
					});
				}else{
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$scope.$broadcast('scroll.refreshComplete');
				}
			}
			$scope.$on('stateChangeSuccess', function() {
				$scope.loadMore();
			});
		}
	})
	.controller('TaskSearchCtrl', function ($scope, $stateParams, $ionicLoading, xhr) {
		var p =1;
		$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
		xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: p, query: $stateParams.query}}, function(resp){
			if(resp.type == 'Success'){
				$scope.tasks = resp.data;
				$scope.totalPages = resp.page.totalPages.value;
			}else{
				$scope.tasks = new Array;
				$scope.totalPages = 0;
			}
			$ionicLoading.hide();
		});
		$scope.doRefresh = function(){
			$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
			xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: 1, query: $stateParams.query}}, function(resp){
				$ionicLoading.hide();
				$scope.tasks = resp.data;
				$scope.$broadcast('scroll.refreshComplete');
			});
		};
		$scope.loadMore = function(){
			if($scope.totalPages > ++p){
				$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
				xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: p, query: $stateParams.query}}, function(resp){
					$ionicLoading.hide();
					$scope.tasks.push(resp.data);
					$scope.$broadcast('scroll.infiniteScrollComplete');
				});
			}else{
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$scope.$broadcast('scroll.refreshComplete');
			}
		}
		$scope.$on('stateChangeSuccess', function() {
			$scope.loadMore();
		});
	})
	.controller('categoryListCtrl', function($scope, $stateParams, $ionicLoading, xhr){
		var p =1;
		$scope.categoryTitle = $stateParams.name;
		$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
		xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: p, cid: $stateParams.cid}}, function(resp){
			$ionicLoading.hide();
			$scope.tasks = resp.data;
			$scope.totalPages = resp.page.totalPages.value;
		});
		$scope.doRefresh = function(){
			xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: 1, cid: $stateParams.cid}}, function(resp){
				$scope.tasks.push(resp.data);
			}).finally(function(){
				$scope.$broadcast('scroll.refreshComplete');
			});
		};
		$scope.loadMore = function(){
			p++;
			if($scope.totalPages > p){
				xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: p, cid: $stateParams.cid}}, function(resp){
					$scope.tasks.push(resp.data);
					$scope.$broadcast('scroll.infiniteScrollComplete');
				});
			}else{
				$ionicLoading.show({ content: '已到达最后', animation: 'fade-in', showBackdrop: true, duration: 2000});
			}
		}
		$scope.$on('stateChangeSuccess', function() {
			$scope.loadMore();
		});
	})
	.controller('TaskDetailCtrl', function($scope, $stateParams, $state, $ionicLoading, xhr, ngSwal){
		$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
		xhr.service('get', {model: 'MustachTask', module: 'getTaskInfo', data: {id: $stateParams.id}}, function(resp){
			xhr.userState('list', function(user, identity){
				var storage = xhr.getStorage();
				$ionicLoading.hide();
				$scope.task = resp.data;
				$scope.state = identity;
				$scope.user = user;
				$scope.isTender = function(){
					return ($scope.state.isLogin && $scope.state.isEngineer && (storage.user.uid != resp.data.uid))?true:false;
				}
				if($scope.state.isLogin){
					var data = {tid: $stateParams.id, uid: storage.user.id};
					xhr.service('get', {model: 'MustachTask', module: 'getFavoriteTaskInfo', data: data}, function (resp) {
						$scope.favoriteTitle = (resp.type=='Success')?'取消收藏':'收藏任务';
					})
				}
				$scope.isFavorite = function(){
					return ($scope.state.isLogin &&  (storage.user.uid != resp.data.uid))?true:false;
				}
				$scope.task_tender = function () {
					ngSwal.prompt({
						title: '立即抢单',
						text: '请对该任务如果交由您来做的一些文字说明（让客户了解您的想法，有利于中标哦！）'
					}, function (inputValue){
						var data = {tid: $stateParams.id, uid: storage.user.id, realname: storage.user.realname, mobile: storage.user.mobile, explain: inputValue};
						$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
						xhr.service('post', {model: 'MustachTask', module: 'taskTender', data:data}, function (rep) {
							$ionicLoading.hide();
							$state.go('app.tasks', {}, {reload: true});
						});
					})
				}
				$scope.task_favorite = function(){
					var data = {tid: $stateParams.id, uid: storage.user.id};
					$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
					xhr.service('post', {model: 'MustachTask', module: 'taskFavoriteEngineerAction', data: data}, function(){
						$ionicLoading.hide();
					})
				}
			});
		})
	})
	.controller('myPushTaskCtrl', function($scope, $ionicLoading, xhr){
		xhr.userState('login', function(user, identity){
			$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
			var storage = xhr.getStorage();
			xhr.service('get', {model: 'MustachTask', module: 'getAllPushTaskList', data: {uid: storage.user.id}}, function(resp){
				$ionicLoading.hide();
				$scope.groups = resp.data;
				$scope.toggleGroup = function(item) {
					item.show = !item.show;
				};
				$scope.isGroupShown = function(item) {
					return item.show;
				};
				$scope.affirm = function(tid, uid){
					console.log(tid, uid);
				}
			});
		}, true);
	})
	.controller('myTenderTaskCtrl', function($scope, $ionicLoading, xhr, ngSwal){
		xhr.userState('engineer', function(user, identity){
			$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
			var storage = xhr.getStorage();
			xhr.service('get', {model: 'MustachTask', module: 'getAllTenderTaskList', data: {uid: storage.user.id}}, function(resp){
				$ionicLoading.hide();
				$scope.groups = resp.data;
				$scope.toggleGroup = function(item) {
					item.show = !item.show;
				};
				$scope.isGroupShown = function(item) {
					return item.show;
				};
				$scope.affirm = function(tid, uid){
					console.log(tid, uid);
				}
				$scope.reject = function(id){
					ngSwal.confirm({text: '您确认要拒绝这个任务吗?'}, function () {
						console.log(id);
					});
				}
			});
		}, true);
	})
	.controller('EngineersCtrl', function($scope,  $timeout, $http, $ionicFilterBar, $ionicLoading, Engineers, xhr){
		var filterBarInstance;
		$scope.showFilterBar = function () {
			filterBarInstance = $ionicFilterBar.show({
				items: $scope.items,
				cancelText: '取消',
				update: function (filteredItems) {
					$scope.items = filteredItems;
				}
			});
		};
		var p =1;
		var storage = xhr.getStorage();
		if((angular.isUndefined(storage.category) || storage.category === null)){
			$http({
				url: baseUrl + '/Restful/angular.html',
				cache: false,
				timeout: 30000,
				method: 'get',
				params: {model: 'MustachTask', module: 'categoryCache', data: {reload: true}}
			}).success(function(resp){
				if(resp.type == 'Success'){
					$scope.categoryList = resp.data;
				}
			});
		}else{
			$scope.categoryList = storage.category.data;
		}
		$scope.onSlideMove = function(index){
			//$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
			xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: p, cid: index.cid}}, function(resp){
				if(resp.type == 'Success'){
					$scope.tasks = resp.data;
					$scope.totalPages = resp.page.totalPages.value;
				}else{
					$scope.tasks = new Array;
					$scope.totalPages = 0;
				}
				$ionicLoading.hide();
			});
			$scope.doRefresh = function(){
				$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
				xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: 1, cid: index.cid}}, function(resp){
					if(resp.type == 'Success'){
						$scope.tasks = resp.data;
						$scope.totalPages = resp.page.totalPages.value;
					}else{
						$scope.tasks = new Array;
						$scope.totalPages = 0;
					}
					$ionicLoading.hide();
					$scope.$broadcast('scroll.refreshComplete');
				});
			};
			$scope.loadMore = function(){
				if($scope.totalPages > ++p){
					$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
					xhr.service('get', {model: 'MustachTask', module: 'getTaskList', data: {page: p, cid: index.cid}}, function(resp){
						if(resp.type == 'Success'){
							$scope.tasks = resp.data;
							$scope.totalPages = resp.page.totalPages.value;
						}else{
							$scope.tasks = new Array;
							$scope.totalPages = 0;
						}
						$ionicLoading.hide();
						$scope.$broadcast('scroll.infiniteScrollComplete');
					});
				}else{
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$scope.$broadcast('scroll.refreshComplete');
				}
			}
			$scope.$on('stateChangeSuccess', function() {
				$scope.loadMore();
			});
		}
		$scope.refreshItems = function () {
			if (filterBarInstance) {
				filterBarInstance();
				filterBarInstance = null;
			}
			$timeout(function () {
				$scope.$broadcast('scroll.refreshComplete');
			}, 1000);
		};
		$scope.engineers = Engineers.all();
		/*
		$scope.doRefresh = function(){
			$http.get('json/engineer.json')
				.success(function(newItems){
					$scope.items = newItems;
				})
				.finally(function(){
					$scope.$broadcast('scroll.refreshComplete');
				});
		};
		*/
	})
	.controller('EngineerDetailCtrl', function ($scope, $stateParams, Engineers) {
		$scope.engineer = Engineers.get($stateParams.engineerId);
	})
	.controller('helpsCtrl', function ($scope, xhr) {
		var storage = xhr.getStorage();
		$scope.service = storage.sitedate.data.service;
		$scope.toggleGroup = function(item) {
			item.show = !item.show;
		};
		$scope.isGroupShown = function(item) {
			return item.show;
		};
	})
	.controller('questionCtrl', function ($scope, xhr) {
		var storage = xhr.getStorage();
		$scope.question = storage.sitedate.data.question;
		$scope.toggleGroup = function(item) {
			item.show = !item.show;
		};
		$scope.isGroupShown = function(item) {
			return item.show;
		};
	})
	.controller('favoriteCtrl', function($scope, xhr){
		"我的收藏控制器";
		xhr.userState('login', function(user, identity){
			$scope.identity = identity;
			xhr.service('get', {model: 'MustachTask', module: 'getFavoriteEngineerList', data: {uid: user.id}}, function(resp){
				if(resp.type=='Success')
					$scope.favoriteEngineer = resp.data;
				else
					$scope.favoriteEngineer = new Array;
			});
			if($scope.identity.isEngineer){
				xhr.service('get', {model: 'MustachTask', module: 'getFavoriteTaskList', data: {uid: user.id}}, function(resp){
					if(resp.type=='Success')
						$scope.favoriteTasks = resp.data;
					else
						$scope.favoriteTasks = new Array;
				});
			}
		}, true);
	})
	.controller('IntroCtrl', function($scope, $state, $ionicSlideBoxDelegate) {
		
		// Called to navigate to the main app
		$scope.startApp = function() {
			$state.go('app.home');
		};
		$scope.next = function() {
			$ionicSlideBoxDelegate.next();
		};
		$scope.previous = function() {
			$ionicSlideBoxDelegate.previous();
		};
		
		// Called each time the slide changes
		$scope.slideChanged = function(index) {
			$scope.slideIndex = index;
		};
	});